package com.simulation.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simulation.common.constant.Constants;
import com.simulation.web.domain.TechCollectionPost;
import com.simulation.web.domain.TechPost;
import com.simulation.web.dto.TechCollectionDTO;
import com.simulation.web.mapper.CollectionPostMapper;
import com.simulation.web.service.CollectionPostService;
import com.simulation.web.service.PostService;
import com.simulation.web.utils.PostStatusEnum;
import com.simulation.web.vo.TechPostVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class CollectionPostServiceImpl implements CollectionPostService {

    private final CollectionPostMapper collectionPostMapper;

    private final PostService postService;

    @Override
    public PageInfo<TechCollectionPost> getByCollectionId(TechCollectionDTO techCollectionDTO) {
        PageHelper.startPage(techCollectionDTO.getPageNum(), techCollectionDTO.getPageSize());
        List<TechCollectionPost> postList = collectionPostMapper.getByCollectionId(techCollectionDTO.getId());
        return new PageInfo<>(postList);
    }

    @Override
    public int insert(Long collectionId, List<Long> postIdList) {
        // 删除旧的关联关系
        deleteByCollectionId(collectionId);

        if (CollectionUtil.isEmpty(postIdList)) {
            return Constants.INT_ZERO;
        }

        // 复制新项目
        List<Long> newPostIdList = getNewPost(collectionId, postService.getByIdList(postIdList));

        // 保存新的关联关系
        return collectionPostMapper.insert(collectionId, newPostIdList);
    }

    @Override
    public int insertOne(Long collectionId, List<Long> postIdList) {
        // 复制新项目
//        List<Long> newPostIdList = getNewPost(collectionId, postService.getByIdList(postIdList));
        // 保存新的关联关系
        return collectionPostMapper.insert(collectionId, postIdList);
    }

    @Override
    public int delete(Long collectionId, List<Long> postIdList) {
        // 删除项目信息
        if (CollectionUtil.isNotEmpty(postIdList)) {
            postService.deleteById(postIdList);
        }
        return collectionPostMapper.delete(collectionId, postIdList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByCollectionId(Long collectionId) {
        // 删除项目信息
        List<Long> postIdList = getPostByCollectionId(collectionId);
        if (CollectionUtil.isNotEmpty(postIdList)) {
            postService.deleteById(postIdList);
        }
        // 删除关联关系
        return collectionPostMapper.deleteByCollectionId(collectionId);
    }

    @Override
    public List<TechCollectionPost> getListByCollectionId(Long collectionId) {
       return collectionPostMapper.getByCollectionId(collectionId);
    }

    @Override
    public List<Long> getPostByCollectionId(Long id) {
        List<TechCollectionPost> techCollectionPostList = collectionPostMapper.getPostByCollectionId(id);
        return techCollectionPostList.stream().map(TechCollectionPost::getPostId).collect(Collectors.toList());
    }

    private List<Long> getNewPost(Long collectionId, List<TechPostVO> techPostVOList) {
        List<Long> newPostIdList = new ArrayList<>();
        for (TechPostVO techPostVO : techPostVOList) {
            TechPost techPost = new TechPost();
            BeanUtil.copyProperties(techPostVO, techPost);
            techPost.setCollectionId(collectionId);
            techPost.setStatus(PostStatusEnum.COLLECTION.name());
            postService.copyPost(techPost);
            newPostIdList.add(techPost.getId());
        }
        return newPostIdList;
    }
}